﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Brain.Build;
using System.IO;

namespace Brain.Editor
{
    public partial class AssetManager : Form
    {
        public AssetManager()
        {
            InitializeComponent();
            Refresh();
        }

        public AssetManager(bool accept)
        {
            InitializeComponent();

            this.button1.Visible = accept;
            Refresh();
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = @"All files|*.*";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = fileDialog.FileName;
                string fileName = Path.GetFileName(file);
                string finalPlace = Path.Combine(Path.Combine(Engine.Instance.SerializationManager.LevelData.RootFolder, "Content"), fileName);
                if (!File.Exists(finalPlace))
                {
                    File.Copy(file, finalPlace);
                }
                ContentProjectFile c = new ContentProjectFile();
                c.FileExtension = Path.GetExtension(file);
                c.Name = Path.GetFileNameWithoutExtension(file);
                c.Project = Engine.Instance.SerializationManager.LevelData.ContentProject;
                c.RelativePath = Path.GetFileName(finalPlace);
                ContentFileUtil.InsertDefaultProcessors(c.FileExtension, out c.Processor, out c.Importer);

                ContentBuilder builder = new ContentBuilder();
                builder.Add(file, c.Name, c.Importer, c.Processor, null);
                string building = builder.Build();
                if (string.IsNullOrEmpty(building))
                {
                    File.Copy(Path.Combine(builder.OutputDirectory, c.Name + ".xnb"),
                        Path.Combine(Path.Combine(Path.Combine(Engine.Instance.SerializationManager.LevelData.RootFolder, "Content"), "Temp"), c.Name + ".xnb"), true);
                }
                string proc = Path.Combine(Path.Combine(Engine.Instance.SerializationManager.LevelData.RootFolder, "Content"), "Temp");
                c.ProcessedPath = Path.Combine(proc, c.Name);

                builder.Dispose();

                if (string.IsNullOrEmpty(building))
                {
                    Engine.Instance.SerializationManager.LevelData.ContentProject.Files.Add(c);
                    Refresh();
                }
                else
                {
                    MessageBox.Show("Couldn't compile your file!");
                }
            }
        }

        private void Refresh()
        {
            this.listBox_Assets.Items.Clear();

            for (int i = 0; i < Engine.Instance.SerializationManager.LevelData.ContentProject.Files.Count; i++)
            {
                ContentProjectFile file = Engine.Instance.SerializationManager.LevelData.ContentProject.Files[i];
                this.listBox_Assets.Items.Add(file);
            }
        }

        /// <summary>
        /// Previews the Selected content file
        /// </summary>
        private void PreviewContent(ContentProjectFile file)
        {
            PreviewForm form = new PreviewForm(file);
            form.ShowDialog();
        }

        private void button_Preview_Click(object sender, EventArgs e)
        {
            if (listBox_Assets.SelectedItem is ContentProjectFile)
            {
                PreviewContent(listBox_Assets.SelectedItem as ContentProjectFile);
            }
        }

        public ContentProjectFile SelectedFile;
        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox_Assets.SelectedItem is ContentProjectFile)
            {
                SelectedFile = listBox_Assets.SelectedItem as ContentProjectFile;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
    }
}
